package com.cooper.controllers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.constants.DisTypeConstants;
import com.cooper.core.JsonResult;
import com.cooper.core.MinasPropertyHandler;
import com.cooper.entity.DisplayPicConfigListDo;
import com.cooper.pagination.PageInfo;
import com.cooper.pagination.PageResult;
import com.cooper.service.admin.DisplayPicConfigService;
import com.cooper.service.project.ProjectService;
import com.cooper.service.seller.DesignerService;
import com.cooper.util.StringUtils;
import com.cooper.vo.CompositeDesignerProjectVo;
import com.cooper.vo.DisplayPicConfigVo;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.portal.Portal;

public class ProjectController {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	DesignerService  disignerService;
	
	@Autowired
	ProjectService projectService;
	
	@Autowired
	private MinasPropertyHandler minasPropertyHandler;
	
	@Get
	public String content(Invocation inv, Portal portal) throws Exception{
		//inv.addModel("dataList", this.adminMenuService.getAllMenus());
		DisplayPicConfigVo queryParamVo = new DisplayPicConfigVo();
		queryParamVo.setStatus(DisplayPicConfigListDo.DISPLAY_YES);
		queryParamVo.setDisType(DisTypeConstants.PROJECT_HEAD);
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageIndex(1);
		pageInfo.setPageSize(6);
		PageResult<DisplayPicConfigVo> ret = displayPicConfigService.queryDisplayPicConfigPage(queryParamVo, pageInfo);
		if(ret!=null){
			inv.addModel("projectHeadList", ret.getResult());
		}
		portal.addWindow("projectFirstListWindow", "/project/projectFirstListWindow");
		portal.addWindow("institutionListWindow", "/project/institutionListWindow");
		portal.addWindow("designerFirstListWindow", "/project/designerFirstListWindow");
		return "project";
	}
	
	@Autowired 
	DisplayPicConfigService displayPicConfigService;
	
	@Get("designerFirstList")
	public Object designerFirstList(Invocation inv){
		JsonResult json = new JsonResult();
		json.setSuccess(true);
		try{
			CompositeDesignerProjectVo queryParamVo = new CompositeDesignerProjectVo();
			PageInfo pageInfo = new PageInfo();
			pageInfo.setPageIndex(1);
			pageInfo.setPageSize(3);
			queryParamVo.setDisplay(1);
			PageResult<CompositeDesignerProjectVo> ret = disignerService.findCompositeDesignerProjects(queryParamVo, pageInfo);
			if(ret!=null){
				json.put("designerFirstList", ret.getResult());
			}
		}catch(Exception e){
			logger.error("ProjectController-designerFirstList-error",e);
			json.setSuccess(false);
		}
		
		return json;
	}
	
	@Get("designerFirstListWindow")
	public Object designerFirstListWindow(Invocation inv){
		StringBuffer buf = new StringBuffer();
		try{
			CompositeDesignerProjectVo queryParamVo = new CompositeDesignerProjectVo();
			PageInfo pageInfo = new PageInfo();
			pageInfo.setPageIndex(1);
			pageInfo.setPageSize(3);
			queryParamVo.setDisplay(1);
			PageResult<CompositeDesignerProjectVo> ret = disignerService.findCompositeDesignerProjects(queryParamVo, pageInfo);
			if(ret!=null&&ret.getResult()!=null){
				for(CompositeDesignerProjectVo item:ret.getResult()){
					 buf.append("<div class='designerOne designerArea left'><a href='seller/sellerHome?sellerId="+item.getUserId()+"'><img class='designerPhoto center' src="+this.minasPropertyHandler.getFileServer()+"download/").append(item.getSellerFileId()).append(" alt='' /></a><div class='designerName fontColorGray en_M textCenter'>");
					 buf.append(item.getSellerName()).append("</div><div class='designerAddress fontColorGray en_SS textCenter'>")
					 .append(item.getStrLocationNames()).append("</div><div class='designerBriefInfo fontColorGray en_SS'>").append(item.getDescription()==null?"":item.getDescription()).append("</div></div>");
				}
			}
		}catch(Exception e){
			logger.error("ProjectController-designerFirstList-error",e);
		}
		return "@"+buf.toString();
	}
	
	@Get("projectFirstList")
	public Object projectFirstList(Invocation inv){
		JsonResult json = new JsonResult();
		json.setSuccess(true);
		try{
			CompositeDesignerProjectVo queryParamVo = new CompositeDesignerProjectVo();
			PageInfo pageInfo = new PageInfo();
			pageInfo.setPageIndex(1);
			pageInfo.setPageSize(7);
			PageResult<CompositeDesignerProjectVo> ret = projectService.findCompositeDesignerProjects(queryParamVo, pageInfo);
			if(ret!=null){
				json.put("projectFirstList", ret.getResult());
			}
		}catch(Exception e){
			logger.error("ProjectController-projectFirstList-error",e);
			json.setSuccess(false);
		}
		
		return json;
	}
	
	@Get("projectFirstListWindow")
	public String projectFirstListWindow(){
		StringBuffer buf = new StringBuffer();
		try{
			CompositeDesignerProjectVo queryParamVo = new CompositeDesignerProjectVo();
			PageInfo pageInfo = new PageInfo();
			pageInfo.setPageIndex(1);
			pageInfo.setPageSize(7);
			PageResult<CompositeDesignerProjectVo> ret = projectService.findCompositeDesignerProjects(queryParamVo, pageInfo);
			if(ret!=null&&ret.getResult()!=null){
				for(CompositeDesignerProjectVo item:ret.getResult()){
					 buf.append("<li><img src="+this.minasPropertyHandler.getFileServer()+"download/").append(item.getProjectFileId()).append("/><div class='projectPosterInfo'>");
				     buf.append(item.getDescription()).append("</div><a href='#'><div class='projectLink'>READ MORE</div></a></li>");
				}
			}
		}catch(Exception e){
			logger.error("ProjectController-projectFirstList-error",e);
		}
		
		return "@"+buf.toString();
	}
	
	
	
	@Get("institutionList")
	public Object institutionList(Invocation inv){
		JsonResult json = new JsonResult();
		json.setSuccess(true);
		try{
			DisplayPicConfigVo queryParamVo = new DisplayPicConfigVo();
			queryParamVo.setStatus(DisplayPicConfigListDo.DISPLAY_YES);
			queryParamVo.setDisType(DisTypeConstants.PROJECT_INSTITUTION);
			PageInfo pageInfo = new PageInfo();
			pageInfo.setPageIndex(1);
			pageInfo.setPageSize(6);
			PageResult<DisplayPicConfigVo> ret = displayPicConfigService.queryDisplayPicConfigPage(queryParamVo, pageInfo);
			if(ret!=null){
				json.put("institutionList", ret.getResult());
			}
		}catch(Exception e){
			logger.error("ProjectController-institutionList-error",e);
			json.setSuccess(false);
		}
		
		return json;
	}
	
	@Get("institutionListWindow")
	public String institutionListWindow(){
		StringBuffer buf = new StringBuffer();
		try{
			DisplayPicConfigVo queryParamVo = new DisplayPicConfigVo();
			queryParamVo.setStatus(DisplayPicConfigListDo.DISPLAY_YES);
			queryParamVo.setDisType(DisTypeConstants.PROJECT_INSTITUTION);
			PageInfo pageInfo = new PageInfo();
			pageInfo.setPageIndex(1);
			pageInfo.setPageSize(6);
			PageResult<DisplayPicConfigVo> ret = displayPicConfigService.queryDisplayPicConfigPage(queryParamVo, pageInfo);
			if(ret!=null&&ret.getResult()!=null){
				for(DisplayPicConfigVo item:ret.getResult()){
					 buf.append("<li><img src="+this.minasPropertyHandler.getFileServer()+"download/").append(item.getFileId()).append(" alt='' /><div class='institutionName'>");
				     buf.append(item.getTitle()).append("</div><div class='institutionAddress'>").append(StringUtils.toString(item.getComments())+"</div></li>");
				}
			}
		}catch(Exception e){
			logger.error("ProjectController-institutionList-error",e);
		}
		
		return "@"+buf.toString();
	}
	
}
